home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
dates.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
6KB
|
184 lines
begin
% *****************************************
% * *
% * Proposed solution to exercise 6.7: *
% * *
% *****************************************
procedure prompt_for_date(prompt, year, month, day);
name year, month, day;
text prompt; integer year, month, day;
begin
outtext(prompt);
year := prompt_for_int(" Year? ");
month := prompt_for_int(" Month? ");
day := prompt_for_int(" Day? ");
end;
% An auxiliary procedure used in the procedure prompt_for_date:
integer procedure prompt_for_int(prompt); text prompt;
begin
outtext(prompt); breakoutimage; inimage;
prompt_for_int := inint;
end;
% *****************************************
% * *
% * Proposed solution to exercise 6.9: *
% * *
% *****************************************
Boolean procedure leap_year(year); integer year;
leap_year := mod(year, 4) = 0 and
(mod(year, 100) ne 0 or mod(year, 400) = 0);
% ********************************************
% * *
% * Proposed solution to exercise 6.10.a: *
% * *
% ********************************************
procedure date(year, day_in_year, legal_date, month, day_in_month);
name legal_date, month, day_in_month;
integer year, day_in_year, month, day_in_month;
Boolean legal_date;
begin Boolean month_found;
if day_in_year < 1 or day_in_year > 366 or
(not leap_year(year) and day_in_year = 366)
then legal_date := false
else begin
legal_date := true;
month_found := false;
month := 1;
while not month_found do
if month_start(month) + month_length(month) > day_in_year
then month_found := true
else month := month + 1;
day_in_month := day_in_year - month_start(month) + 1;
end;
end of date;
% Auxiliary arrays (used in the procedure date above):
integer array month_length, month_start(1 : 12);
% A procedure that initializes these two arrays:
procedure Set_months(year); integer year;
begin integer m;
month_length( 1) := 31;
month_length( 2) := if leap_year(year) then 29 else 28;
month_length( 3) := 31; month_length( 4) := 30;
month_length( 5) := 31; month_length( 6) := 30;
month_length( 7) := 31; month_length( 8) := 31;
month_length( 9) := 30; month_length(10) := 31;
month_length(11) := 30; month_length(12) := 31;
month_start(1) := 1;
for m := 2 step 1 until 12 do
month_start(m) := month_start(m - 1) + month_length(m - 1);
end;
% A procedure that tests the procedure date above: ;
procedure test_date;
begin
integer year, y_day, month, m_day;
Boolean day_ok;
outtext("Test of ""date"": ");
year := prompt_for_int("Year? "); Set_months(year);
y_day := prompt_for_int("Day? ");
date(year, y_day, day_ok, month, m_day);
if not day_ok
then outtext("Impossible day!")
else begin
outtext("That is day number "); write_int(m_day);
outtext(" in month number "); write_int(month);
outtext(" in the year "); write_int(year);
end;
outimage;
end;
% An auxiliary procedure used in the procedure test_date:
procedure write_int(num); integer num;
begin integer numlen, i;
numlen := if num le 0 then 1 else 0;
i := abs(num);
while i > 0 do
begin numlen := numlen + 1; i := i//10 end;
outint(num, numlen);
end;
% ********************************************
% * *
% * Proposed solution to exercise 6.10.c: *
% * *
% ********************************************
procedure opposite_of_date(year, month, day_in_month, legal_date, day_in_year);
name legal_date, day_in_year;
integer year, month, day_in_month, day_in_year;
Boolean legal_date;
begin
if month < 1 or else month > 12
or else day_in_month < 1 or else day_in_month > month_length(month)
then legal_date := false
else begin
legal_date := true;
day_in_year := month_start(month) + day_in_month - 1;
end;
end of opposite_of_date;
% A procedure that tests the procedure opposite_of_date above:
procedure test_opposite_of_date;
begin
integer year, y_day, month, m_day;
Boolean day_ok;
outtext("Test of ""opposite_of_date"": ");
year := prompt_for_int("Year? "); Set_months(year);
month := prompt_for_int("Month? ");
m_day := prompt_for_int("Day? ");
opposite_of_date(year, month, m_day, day_ok, y_day);
if not day_ok then
outtext("Impossible day!")
else begin
outtext("That is day number "); write_int(y_day);
outtext(" in the year "); write_int(year);
end;
outimage;
end;
% *********************************************
% * *
% * Proposed solution to exercise 6.7b, d: *
% * *
% *********************************************
Boolean more_testing; character c;
outtext("Test of the procedures ""date"" and ""opposite_of_date"".");
outimage;
more_testing := true;
while more_testing do
begin
outtext("Type d for oate-test, o for opposite_of_date-test, "
"q for quit> "); breakoutimage;
inimage; c := inchar;
if c = 'd' then test_date else
if c = 'o' then test_opposite_of_date else
if c = 'q' then more_testing := false;
end;
outtext("Bye"); outimage;
end